package com.cxrj.zhsq.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SqlCondition {
	private static Pattern linePattern = Pattern.compile("_(\\w)");
	private static Pattern humpPattern = Pattern.compile("[A-Z]");
	
	/**
     * 获取分页信息
     * */
	public static Map<String, Object> getBaseCondition(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> sqlMap = new HashMap<String, Object>();

		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		String parameter = request.getParameter("parameter");
		String columns = request.getParameter("columns");
		String order = request.getParameter("order");

		/*if (StringUtils.isNotBlank(parameter)) {
			String nowDate = JSONObject.parseObject(parameter).getString("nowDate");
			if(StringUtils.isNotBlank(nowDate)) {
				sqlMap.put("nowDate", nowDate);
				sqlMap.put("startDate", nowDate+"-01-01 00:00:00");
				sqlMap.put("endDate", nowDate+"-12-31 23:59:59");
			}
			
			loopSqlMap(sqlMap, JSONObject.parseObject(parameter));
		}*/
		
		if (StringUtils.isNotBlank(order)) {
			orderSqlMap(sqlMap, JSONObject.parseObject(order));
		}

//		if (StringUtils.isNotBlank(start)) sqlMap.put("columns", humpToLine2(columns));
		if (!"-1".equals(length) && StringUtils.isNotBlank(start)) sqlMap.put("start_page", Integer.parseInt(start));
		if (!"-1".equals(length) && StringUtils.isNotBlank(length)) sqlMap.put("end_page", Integer.parseInt(length));
		
		return sqlMap;
	}
	
	private static void loopSqlMap(Map<String, Object> sqlMap, JSONObject json) {
		for (Iterator<Entry<String, Object>> iterator = json.entrySet().iterator(); iterator.hasNext();) {
			Entry<String, Object> entry = iterator.next();
			String key = entry.getKey();
			Object value = entry.getValue();
			if (value == null) {
				continue;
			}
//			if ("orderColumn".equals(key) || "orderType".equals(key)) {
//				sqlMap.put(key, humpToLine2(value.toString()));
//			} else {
				sqlMap.put(key, value.toString());
//			}
		}
	}

	private static void orderSqlMap(Map<String, Object> sqlMap, JSONObject json) {
		Object orderColumn = json.get("orderColumn");
		Object orderType = json.get("orderType");

		if (orderColumn == null) {
			return;
		}

		String[] orderColumnArray = orderColumn.toString().split(",");
		String[] orderTypeArray;
		String orderBy = "";

		if (orderType != null && StringUtils.isNotBlank(orderType.toString())) {
			orderTypeArray = orderType.toString().split(",");
		} else {
			orderTypeArray = new String[5];
			Arrays.fill(orderTypeArray, "asc");
		}

		for (int i = 0; i < orderColumnArray.length; i++) {
			orderBy += " " + orderColumnArray[i] + " " + orderTypeArray[i] + " ,";
		}
		sqlMap.put("orderBy", orderBy.substring(0, orderBy.length() - 1));
	}
	
    /**
     * 下划线转驼峰
     * */
    public static String lineToHump(String str) {
    	if (StringUtils.isBlank(str)) {
			return null;
		}
    	
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    
    /**
     * 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)})
     * */
    public static String humpToLine(String str) {
    	if (StringUtils.isBlank(str)) {
			return null;
		}
    	
        return str.replaceAll("[A-Z]", "_$0").toLowerCase();
    }
    
    /**
     * 驼峰转下划线,效率比{@link #humpToLine(String)}高
     * */
    public static String humpToLine2(String str) {
		if (StringUtils.isBlank(str)) {
			return null;
		}
    	
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    
}
